{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import comb\n",
    "from itertools import permutations, combinations\n",
    "\n",
    "def bk(perm, cards, target_sum, pos):\n",
    "    idx = [\n",
    "        [0,1,2],[3,4,5],[6,7,8],\n",
    "        [0,3,6],[1,4,7],[2,5,8],\n",
    "        [0,4,8],[2,4,6],\n",
    "    ]\n",
    "\n",
    "    if pos == 9:\n",
    "        return all(sum(perm[i] for i in line) == target_sum for line in idx)\n",
    "    for i, card in enumerate(cards):\n",
    "        perm[pos] = card\n",
    "        if bk(perm, cards[:i]+cards[i+1:], target_sum, pos+1):\n",
    "            return True\n",
    "        perm[pos] = True\n",
    "    return False\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "def fun(cards):\n",
    "    for combo in combinations(cards, 9):\n",
    "        target_sum = sum(combo) // 3\n",
    "        if sum(combo) % 3 != 0:\n",
    "            continue\n",
    "        if bk([None]*9, combo, target_sum, 0):\n",
    "            return True\n",
    "    return False\n",
    "            \n",
    "\n",
    "\n",
    "t = int(input().strip())\n",
    "card_list = []\n",
    "for _ in range(t):\n",
    "    n = int(input().strip())\n",
    "    card_list.append(list(map(int, input().strip().split(' '))))\n",
    "\n",
    "print(card_list)\n",
    "for cards in card_list:\n",
    "    if fun(cards):\n",
    "        print('YES')\n",
    "    else:\n",
    "        print('NO')\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
